//
//  MonitorViewController.m
//  VCS_WS_ClientTest
//
//  Created by hu_danyuan on 18/11/14.
//  Copyright © 2018年 hudanyuan. All rights reserved.
//

#import <GLKit/GLKit.h>
#import <AVPlaySDK/AVPlaySDK.h>
#import <VCS_WS_ClientSDK/VCS_WS_ClientSDK.h>

#import "MonitorViewController.h"

#define MonitorVideoDefaultWidth   0 // 默认视频宽度
#define MonitorVideoDefaultHeight  0 // 默认视频高度

#define MAX_SUPPORT_WIDTH            2000    // 最大支持码流宽度
#define MAX_SUPPORT_HEIGHT            1280    // 最大支持码流高度
#define IS_IPHONEXSERIOUS @available(iOS 11.0, *) && UIApplication.sharedApplication.keyWindow.safeAreaInsets.bottom > 0.0  // 判断是否是刘海屏
#define MAKE_VCS_URL(x) [NSURL URLWithString:[NSString stringWithFormat:@"ws://%@:3098", x]]

@implementation MonitorViewController

- (id)init {
    if (self = [super init]) {
        // Custom initialization
        //self.wantsFullScreenLayout = YES;
        _nAudioDecoderType = -1;
        _nAudioEncoderType = -1;
        _audioStreamId = -1;
        _voiceStreamId = -1;
        _lastEncodeType = VCS_WS_VideoEncodeH264;
        _nStreamQuality = 0;
        _isOpenVideo = NO;
        _isRecording = NO;
        _isOpenVoice = NO;
        _isFirstFrame = YES;
        self.quit = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置背景色
    self.view.backgroundColor = [UIColor blackColor];

    // GLKView
    self.glkView = [[GLKView alloc] initWithFrame:self.view.bounds];
    self.glkView.backgroundColor = [UIColor clearColor];
    [self.view insertSubview:self.glkView atIndex:0];

    [self navigationBarInit];

    // 显示码流信息
    if (self.options[@"showFrameInfo"]) {
        CGRect _videoInfoViewFrame = self.view.bounds;
        _videoInfoView = [[MonitorInfoOverlayView alloc] initWithFrame:_videoInfoViewFrame];
        _videoInfoView.userInteractionEnabled = NO;
        _videoInfoView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [self.view addSubview:_videoInfoView];
    }

    [self initVideoPlay];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];

    // 默认contentMode
    _contentMode = VideoPlayerViewContentModeScaleAspectFit;
    // 屏幕常亮，不待机
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    // 打开视频
    [self startVideo];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self stopVideo];
    [self deinitVideoPlay];
}

- (void)notifyWebView:(NSString *)msg {
    // 判断有没有代理以及代理是否响应代理方法
    if (self.delegate && [self.delegate respondsToSelector:@selector(notifyWebView:)]) {
        [self.delegate notifyWebView:msg];
    }
}

- (void)navigationBarInit{
    int y = 0;
    if (IS_IPHONEXSERIOUS) {
        NSLog(@"is iPhoneXSerious");
        y = 44;
    }

    CGRect frame = CGRectMake(0, y, 100, 80);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = frame;
    [button setTitle:@"返回" forState: UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    button.tintColor = [UIColor whiteColor];
    button.titleLabel.font = [UIFont systemFontOfSize:21];
    button.tag = 2000;
    [button addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    //    //创建一个导航栏y
    //    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, y, self.view.frame.size.width, 60)];
    //
    //    //标题栏的整个背景色改为蓝色
    //    [navigationBar setBarTintColor:[UIColor clearColor]];
    //
    //    //将标题栏中间的主title的文字颜色设置为白色
    //    [navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
    //
    //    //此段设置整个标题栏的背景图片
    //    //    [navigationBar setBackgroundImage:[UIImage imageNamed:@"xx"] forBarMetrics:UIBarMetricsDefault];
    //
    //    //设置左右两边的图片和文字均为白色
    //    [navigationBar setTintColor:[UIColor whiteColor]];
    //
    //    //将导航栏添加入view中
    //    [self.view addSubview:navigationBar];
    //
    //    //新建一个navigationItem
    //    UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
    //
    //    //title为中间的显示，可以是view，可以是文字等等
    //    //    navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    //    //    navigationItem.titleView.backgroundColor = [UIColor yellowColor];
    //
    //    //标题栏中间的文字
    //    navigationItem.title = self.options[@"title"];
    //
    //    //标题栏左侧一个按钮+文字
    //    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    //    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //
    //    //btn.backgroundColor=[UIColor clearColor];
    //    btn.frame = view.frame;
    //    [btn setTitle:@"返回" forState:UIControlStateNormal];
    //    [btn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    //    [view addSubview:btn];
    //
    //    navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:view];
    //    [navigationBar pushNavigationItem:navigationItem animated:YES];
}

- (void)backBtnClick {
    [self notifyWebView:@"VIDEO_CLOSED"];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [self notifyWebView:@"VIDEO_MEMORY_WARING"];
    [super didReceiveMemoryWarning];
}

// 初始化视频相关
- (void)initVideoPlay {
    _nVideoWidth = MonitorVideoDefaultWidth;
    _nVideoHeight = MonitorVideoDefaultHeight;

    NSURL *url = MAKE_VCS_URL(self.options[@"serverIP"]);
    _voiceFrameInfo = [[VCS_WS_AudioFrameInfo alloc] init];
    _voiceFrameInfo.encodeType = VCS_WS_AudioEncodeTypePCM;
    _voiceFrameInfo.sampleRate = 8000;
    _voiceFrameInfo.bitsPerSample = 16;
    _voiceFrameInfo.channelNum = 1;
    _videoMonitor = [[WCS_WS_VideoMonitor alloc] initWithURL:url];
    _videoMonitor.delegate = self;
    [_videoMonitor open];

    // 打开视频播放器
    if (_player == nil) {
        _player = [AVPlaySDK AVPLAY_OpenStream:0 DelayTime:(1000 / 12) * 4];
        [AVPlaySDK AVPLAY_Play:_player View:(__bridge void *)self.glkView Scale:[UIScreen mainScreen].scale Callback:self Param:NULL];
        _frameid = 1;
    }

    // 视频码流开关
    //[self initVideoMonitor];
}

- (void)deinitVideoPlay {
    [_videoMonitor close];
    _videoMonitor.delegate = nil;
    _videoMonitor = nil;
    _voiceFrameInfo = nil;
    _audioStreamId = -1;

    // 关闭视频播放
    if (_audioRecorder) {
        _bVoiceComSuccess = NO;
        _voiceStreamId = -1;
        [AVPlaySDK AVPLAY_StopAudioRecord:_audioRecorder];
        _audioRecorder = nil;
    }
    if (_player) {
        [AVPlaySDK AVPLAY_Stop:_player];
        [AVPlaySDK AVPLAY_CloseStream:_player];
        _player = nil;
        _frameid = 1;
    }
}

// 打开视频
- (void)startVideo {
    // 正在打开视频，或者视频已经打开
    if (!_isOpenVideo) {
        [_videoInfoView startRecord];
        //[self doBeforeStartMonitor];
    }
    _isOpenVideo = YES;
}

// 关闭视频
- (void)stopVideo {
    _bVoiceComSuccess = NO;
    _voiceStreamId = -1;
    [AVPlaySDK AVPLAY_StopAudioRecord:_audioRecorder];
    _audioRecorder = nil;
    [_videoMonitor WCS_stopVoiceCom:nil];

    // 关闭音乐播放
    if (_isOpenVoice) {
        _isOpenVoice = NO;
        //[_voiceButton setImage:[UIImage imageNamed:@"VideoVoice.png"] forState:UIControlStateNormal];
    }

    // 关闭视频播放
    if (_isOpenVideo) {
        //[self doBeforeStopMonitor];
        NSString *protocol = self.options[@"protocol"];
        if (protocol == 'wcs')
            [_videoMonitor WCS_stopRealPlay:nil];
        else
            [_videoMonitor stopRealPlay:nil];
        //[self doAfterStopMonitor];
        //[_videoInfoView stopRecord];
        _isOpenVideo = NO;
    }
    _isFirstFrame = YES;
}

// 切换码流质量
- (void)setStreamQuality:(MonitorStreamQuality)quality {
    _nStreamQuality = quality;
}

- (void)videoMonitorDidConnected:(VCS_WS_VideoMonitor *)videoMonitor {
    NSString *username = self.options[@"username"];
    NSString *password = self.options[@"password"];
    NSInteger streamQuality = [self.options[@"streamQuality"] intValue];

    // VCS 格式视频参数
    NSString *realPlay = self.options[@"realPlay"];
    NSString *cameraUuid = self.options[@"cameraUuid"];
    NSInteger channelIndex = [self.options[@"channelIndex"] intValue];

    // WCS 格式视频参数
    NSString *path = self.options[@"path"];
    NSString *nodeUid = self.options[@"nodeUid"];

    WCS_WS_VideoMonitor *WCS_videoMonitor = (WCS_WS_VideoMonitor *)videoMonitor;
    [videoMonitor login:username password:password completion:^(VCS_WS_LoginReply * _Nullable reply, NSError * _Nullable error) {
        if (error) {
            [self notifyWebView:@"VIDEO_ERROR"];
            NSLog(@"realPlay failed: %@", error);
            [self dismissViewControllerAnimated:NO completion:nil];
        }

        if ([[reply.service uppercaseString] isEqualToString:@"WCS"]) {
            // WCS 格式
            WCS_WS_PreviewInfo *previewInfo = [[WCS_WS_PreviewInfo alloc] init];
            previewInfo.devicePath = [path stringByAppendingFormat:@"/%@", nodeUid];
            previewInfo.streamQuality = _nStreamQuality;
            [WCS_videoMonitor WCS_realPlay:previewInfo completion:^(VCS_WS_RealPlayResult * _Nullable result, NSError * _Nullable error) {
                if (error) {
                    [self notifyWebView:@"VIDEO_ERROR"];
                    NSLog(@"realPlay failed: %@", error);
                    [self dismissViewControllerAnimated:NO completion:nil];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (!error) {
                        _audioStreamId = result.stream_id;
                        _isOpenVideo = YES;
                        [self notifyWebView:@"VIDEO_PALY"];
                    }
                    [self didFinishStartingVideo:error?NO:YES];
                });
            }];
        } else {
            // VCS 格式
            [WCS_videoMonitor realPlay:realPlay cameraUuid:cameraUuid channelIndex:channelIndex streamQuality:_nStreamQuality completion:^(VCS_WS_RealPlayResult * _Nullable result, NSError * _Nullable error) {
                if (error) {
                    [self notifyWebView:@"VIDEO_ERROR"];
                    NSLog(@"realPlay failed: %@", error);
                    [self dismissViewControllerAnimated:NO completion:nil];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (!error) {
                        _audioStreamId = result.stream_id;
                        _isOpenVideo = YES;
                        [self notifyWebView:@"VIDEO_PALY"];
                    }
                    [self didFinishStartingVideo:error?NO:YES];
                });
            }];
        }
    }];

    // [videoMonitor login:username password:password completion:^(VCS_WS_LoginReply * _Nullable reply, NSError * _Nullable error) {
    //     [videoMonitor realPlay:realPlay cameraUuid:cameraUuid channelIndex:channelIndex streamQuality:streamQuality completion:^(VCS_WS_RealPlayResult * _Nullable result, NSError * _Nullable error) {
    //         if (!error) {
    //             _isOpenVideo = YES;
    //             [self notifyWebView:@"VIDEO_PALY"];
    //         } else {
    //             [self notifyWebView:@"VIDEO_ERROR"];
    //             NSLog(@"realPlay failed: %@", error);
    //             [self dismissViewControllerAnimated:NO completion:nil];
    //         }
    //     }];
    // }];
}

- (void)videoMonitorDidDisconnected:(VCS_WS_VideoMonitor *)videoMonitor {
    NSLog(@"monitor Closed");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [videoMonitor open];
    });
}

- (void)videoMonitor:(VCS_WS_VideoMonitor *)videoMonitor didFailWithError:(NSError *)error {
    NSLog(@"monitor didFailWithError:%@", error);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [videoMonitor open];
    });
}

/** 接收到通知消息 */
- (void)videoMonitor:(VCS_WS_VideoMonitor *)videoMonitor didReceiveNotify:(NSDictionary *)notifyInfo {
    NSLog(@"notify====%@", notifyInfo);
}

/** 接收到视频帧流数据 */
- (void)videoMonitor:(VCS_WS_VideoMonitor *)videoMonitor didReceiveVideoFrame:(VCS_WS_VideoFrameInfo *)frameInfo data:(NSData *)data {
    // 更新统计信息
    VCS_WS_VideoFrameInfo *originFrameInfo = [frameInfo copy];

    if (frameInfo.height > MAX_SUPPORT_HEIGHT || frameInfo.width > MAX_SUPPORT_WIDTH) { // 最大支持分辨率
        NSLog(@"Not Support Resolution width=%d, height=%d", frameInfo.width, frameInfo.height);
        //return;
    }

    // 调整大小
    if (_nVideoHeight != frameInfo.height || _nVideoWidth != frameInfo.width) {
        _nVideoHeight = frameInfo.height;
        _nVideoWidth = frameInfo.width;
        [self adjustGLKViewFrame:self.contentMode];
    }

    [self setFrameInfo:frameInfo originFrameInfo:originFrameInfo frameSize:data.length];

    if (!self.quit) {
        // 播放视频
        [self playVideo:data frameInfo:frameInfo];

        // 记录视频
        if (_isRecording) {
            [self recordVideo:data frameInfo:frameInfo];
        }
    }
}

/** 接收到音频帧流数据 */
- (void)videoMonitor:(VCS_WS_VideoMonitor *)videoMonitor didReceiveAudioFrame:(VCS_WS_AudioFrameInfo *)frameInfo data:(NSData *)data {
    // 更新frameinfo
    [self setFrameInfo:frameInfo originFrameInfo:[frameInfo copy] frameSize:data.length];

    if (_isOpenVoice == NO) {
        return;
    }

    if (!self.quit && !_isMute) {
        // 如果对讲与预览的语音同时打开，则优先使用对讲的 //
        if (_voiceStreamId != -1 && _audioStreamId != -1 && frameInfo.streamId == _audioStreamId) {
            return;
        }
        // 播放音频
        [self playAudio:data frameInfo:frameInfo];
    }
}

// 视频开启结束
- (void)didFinishStartingVideo:(BOOL)success {
    if (success) {
        _isOpenVideo = YES;
        self.navigationItem.rightBarButtonItem.enabled = NO;
    } else {
        _isOpenVideo = NO;
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
    _isOpenVoice = YES;
    [AVPlaySDK AVPLAY_OpenAudio:_player];
    [AVPlaySDK AVPLAY_SetAudioVolume:_player Volume:100];
}

- (IBAction)muteCall:(UIButton *)sender {
    if (_isMute) {
        [sender setTitle:@"静音" forState:UIControlStateNormal];
        //[_videoMonitor controlAudio:@"enable" completion:nil];
    } else{
        [sender setTitle:@"取消静音" forState:UIControlStateNormal];
        //[_videoMonitor controlAudio:@"disable" completion:nil];
    }
    _isMute = !_isMute;
}

- (IBAction)voiceCall:(UIButton *)sender {
    if (_isVoiceTalk) {
        [sender setTitle:@"打开对讲" forState:UIControlStateNormal];
        _bVoiceComSuccess = NO;
        _voiceStreamId = -1;
        [AVPlaySDK AVPLAY_StopAudioRecord:_audioRecorder];
        _audioRecorder = nil;
        [_videoMonitor WCS_stopVoiceCom:nil];
    } else {
        [sender setTitle:@"关闭对讲" forState:UIControlStateNormal];
        _audioRecorder = [AVPlaySDK AVPLAY_StartAudioRecord:AVPLAY_AUDIO_PCM SampleRate:8000 BitsPerSample:16 ChannelNum:1 Callback:self Param:NULL];
        if (_audioRecorder == NULL) {
            NSLog(@"AVPLAY_StartAudioRecord failed!!!");
        } else {
            // WCS 格式视频参数
            NSString *path = self.options[@"path"];
            NSString *nodeUid = self.options[@"nodeUid"];

            WCS_WS_VoiceComParam *previewInfo = [[WCS_WS_VoiceComParam alloc] init];
            previewInfo.devicePath = [path stringByAppendingFormat:@"/%@", nodeUid];
            [_videoMonitor WCS_startVoiceCom:previewInfo completion:^(VCS_WS_VoiceComResult * _Nullable result, NSError * _Nullable error) {
                if (error) {
                    NSLog(@"startVoiceCom failed(%@)!!!", error);
                } else {
                    _voiceStreamId = result.stream_id;
                    _bVoiceComSuccess = YES;
                }
            }];
        }
    }
    _isVoiceTalk = !_isVoiceTalk;
}

- (BOOL)playVideo:(NSData *)data frameInfo:(VCS_WS_VideoFrameInfo *)frameInfo {
    BOOL ret = NO;
    if (data == nil || frameInfo == nil || _player == nil) {
        return NO;
    }
    ret = [AVPlaySDK AVPLAY_InputData:_player Buf:(AVPLAY_BYTE *)data.bytes Len:(AVPLAY_int32)data.length];
    return ret;
}

- (BOOL)playAudio:(NSData *)data frameInfo:(VCS_WS_AudioFrameInfo *)frameInfo {
    BOOL ret = NO;
    if (data == nil || frameInfo == nil || _player == nil) {
        return NO;
    }
    // 往缓存中送数据
    ret = [AVPlaySDK AVPLAY_InputData:_player Buf:(AVPLAY_BYTE *)data.bytes Len:(AVPLAY_int32)data.length];
    return ret;
}

- (void)recordVideo:(NSData *)data frameInfo:(VCS_WS_VideoFrameInfo *)frameInfo {
}

- (AVPLAY_int32)onSolutionChanged:(AVPLAY_HANDLE)hPlayHandle Width:(AVPLAY_int32)nWidth Height:(AVPLAY_int32)nHeight Param:(void*)pUserParam {
    _nVideoWidth = nWidth;
    _nVideoHeight = nHeight;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self adjustGLKViewFrame:self.contentMode];
    });
    return 0;
}

- (AVPLAY_int32)onPlayPosition:(AVPLAY_HANDLE)hPlayHandle StartTime:(AVPLAY_int32)nStartTime EndTime:(AVPLAY_int32)nEndTime CurTime:(AVPLAY_int32)nCurTime Param:(void*)pUserParam {
    return 0;
}

- (AVPLAY_int32)onLostFrame {
    //NSLog(@"========onLostFrame=========");
    return 0;
}

// bNeedInput: TRUE 表示缓冲区变小，需要继续添加数据；  FALSE表示缓冲区过大，可以暂停添加数据。
- (AVPLAY_int32)onNotifyInputData:(BOOL)bNeedInput {
    return 0;
}

- (AVPLAY_int32)onEmpty {
    return 0;
}

- (AVPLAY_int32) onAudioFrameData: (AVPLAY_HANDLE)hAudioHandle Buf: (AVPLAY_BYTE *)pBuf Len: (AVPLAY_int32)nLen Param: (void*)pUserParam {
    //dispatch_sync(dispatch_get_main_queue(), ^{
    [self _onAudioFrameData:hAudioHandle Buf:pBuf Len:nLen Param:pUserParam];
    //});
    return 0;
}

- (AVPLAY_int32) _onAudioFrameData: (AVPLAY_HANDLE)hAudioHandle Buf: (AVPLAY_BYTE *)pBuf Len: (AVPLAY_int32)nLen Param: (void*)pUserParam {
    if (!_bVoiceComSuccess)
        return 0;
    NSData *data = [NSData dataWithBytesNoCopy:pBuf length:nLen freeWhenDone:NO];
    [_videoMonitor WCS_voiceComSendData:_voiceFrameInfo data:data error:nil];
    return 0;
}

// 设置视频播放模式
- (void)setContentMode:(VideoPlayerViewContentMode)contentMode {
    [self didContenModeChange:contentMode];
    _contentMode = contentMode;
}

- (void)didContenModeChange:(VideoPlayerViewContentMode)contentMode {
    if (contentMode == self.contentMode) {
        return;
    }
    [self adjustGLKViewFrame:contentMode];
}

// 调整视频播放view的frame
- (void)adjustGLKViewFrame:(VideoPlayerViewContentMode)contentMode {
    if (_nVideoHeight == 0 || _nVideoWidth == 0 ||
        self.view.frame.size.width < 0.00001 || self.view.frame.size.height < 0.00001) {
        return;
    }
    CGRect rect = self.view.bounds;
    // 居中rect除去工具栏rect
    //    if (!_toolsBg.hidden && _toolsBg.alpha > 0.1f) {
    //        rect.size.height -= SHMonitorViewToolBarHeight;
    //    }
    float scaleW = self.view.frame.size.width / _nVideoWidth;
    float scaleH = self.view.frame.size.height / _nVideoHeight;
    switch (contentMode) {
        case VideoPlayerViewContentModeOrigin: // 原始
            scaleW = 1.0f;
            scaleH = 1.0f;
            rect.size = CGSizeMake(_nVideoWidth, _nVideoHeight);
            rect.origin.x += (self.view.frame.size.width - _nVideoWidth) / 2;
            rect.origin.y += (self.view.frame.size.height - _nVideoHeight) / 2;
            break;
        case VideoPlayerViewContentModeScaleToFill: // 缩放填充
            rect = self.view.bounds;
            break;
        case VideoPlayerViewContentModeScaleAspectFit: // 等比列放大
            if (scaleH > scaleW) {
                scaleH = scaleW;
                int height = _nVideoHeight * scaleW;
                rect.origin.y += (rect.size.height - height) / 2;
                rect.size.height = height;
            } else {
                scaleW = scaleH;
                int width = _nVideoWidth * scaleH;
                rect.origin.x += (rect.size.width - width) / 2;
                rect.size.width = width;
            }
            break;
        case VideoPlayerViewContentModeScaleAspectFill: // 等比例填充
            if (scaleH > scaleW) {
                scaleW = scaleH;
                int width = _nVideoWidth * scaleH;
                rect.origin.x += (rect.size.width - width) / 2;
                rect.size.width = width;
            } else {
                scaleH = scaleW;
                int height = _nVideoHeight * scaleW;
                rect.origin.y += (rect.size.height - height) / 2;
                rect.size.height = height;
            }
            break;
        default:
            break;
    }
    /*// 暂时处理，视频解码时有问题，导致边界有一条线，这样做是为了隐藏掉那条线
     float lineWidth = 0;
     rect.size.width += lineWidth * scaleW;
     rect.size.height += lineWidth * scaleH;*/
    self.glkView.frame = rect;
    [self.glkView setNeedsDisplay];
}

- (void)setFrameInfo:(NSObject *)frameInfo originFrameInfo:(NSObject *)originFrameInfo frameSize:(NSUInteger)frameSize {
    dispatch_async(dispatch_get_main_queue(), ^{
        [_videoInfoView setFrameInfo:frameInfo originFrameInfo:originFrameInfo frameSize:frameSize];
    });
}

// 即将要动画
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    // [self adjustDynamicView];
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
        toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        self.contentMode = VideoPlayerViewContentModeScaleToFill;
    } else {
        self.contentMode = VideoPlayerViewContentModeScaleAspectFit;
    }
}

@end
